package com.xrui.hbasecmd.format;

import com.xrui.hbasecmd.config.GlobalConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class DateUtils {

    private final static Logger LOG = LoggerFactory.getLogger(DateUtils.class);
    private static final DateFormat df;

    static {
        df = new SimpleDateFormat(GlobalConfig.instance().getDateFormat(), Locale.ENGLISH);
        df.setTimeZone(TimeZone.getTimeZone(GlobalConfig.instance().getDateTimeZone()));
    }

    private DateUtils() {
    }

    public static DateFormat getDefaultDateFormat() {
        return df;
    }

    public static String format(Date date) {
        return df.format(date);
    }

    public static Date parse(String date) {
        try {
            return df.parse(date);
        }
        catch (ParseException e) {
            LOG.error(String.format("Failed to convert Date from string '%s'.", date), e);
            return null;
        }
    }
}
